<template>
  <div class="login">
    <div
      style="
        margin: 180px auto;
        background-color: #fff;
        width: 350px;
        padding: 20px;
        border-radius: 10px;
      "
    >
      <div class="title"> <i class="el-icon-s-promotion"></i> 注册</div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="nickname">
          <el-input
            size="medium"
            style="margin: 10px auto"
            prefix-icon="el-icon-user"
            v-model="user.nickname"
            placeholder="请输入用户名"
          ></el-input
        ></el-form-item>
        <el-form-item prop="password"
          >
          
          <el-input
            size="medium"
            style="margin: 10px auto"
            prefix-icon="el-icon-lock"
            show-password
            v-model="user.password"
            placeholder="请输入密码"
          ></el-input
        >
        </el-form-item>
         <el-form-item prop="mobile"
          >
          <el-input
            size="medium"
            prefix-icon="el-icon-phone-outline"
            v-model="user.mobile"
            style="float: left;clear: both;"
            placeholder="请输入手机号"

          ></el-input
        >
         </el-form-item>
         <!-- 优化高度问题 -->
        <div style="height: 14px;"></div> 
        <el-form-item prop="code"
          >
          <el-input
            size="medium"
            prefix-icon="el-icon-warning-outline"
            v-model="user.code"
            style="width: 260px;float: left;clear: both;"
            placeholder="请输入验证码"
          ></el-input
        >
        <span v-if="!sending" class="checkcode" style="color:red;" @click="getPhoneCode">获取验证码</span>
         <!-- 倒计时 -->
        <span v-if="sending" class="count">{{second}}秒后重试</span>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button
            type="primary"
            size="small"
            autocomplete="off"
            @click="login"
            >注册</el-button
          >
          <el-button type="warning" size="small" autocomplete="off" @click="toLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import RegisterApi from '@/api/register'
export default {
  name: "Register",
  data() {
    return {
      user: {
        nickname: "",
        password: "",
        mobile:'',
        code:''
      },
       checkcode:'',
        sending:false,
        second:60,
      rules: {
        nickname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
         code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            min: 5,
            max: 6,
            message: "长度在6 个字符",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            min: 10,
            max: 11,
            message: "长度在11个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //获取验证码
    getPhoneCode(){
      RegisterApi.registerUser(this.user.mobile).then(res=>{
        if(res.code===20000){
          this.$message.success(res.message);
        }
        else{
          this.$message.error(res.message)
        }
      }) //请求验证码
      this.sending=true;
      const timer= setInterval(()=>{
       this.second--;
       if(this.second<1){
         this.sending=false;
         this.second=60;
         clearInterval(timer);
       }
      },1000)
    },
    toLogin(){
      this.$router.push('/login')
    },
    login() {
      this.$refs['userForm'].validate((valid) => {
        //如果合法
        if (valid) {
          RegisterApi.register(this.user).then(res=>{
            if(res.code===20000){
              this.$message.success(res.message)
              this.user=null;//数据清洗
            }
            else{
              this.$message.error(res.message)
            }
          })
        }
        //非法
        else {
            return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.login {
  height: 100vh;
  overflow: hidden;
  background-image: linear-gradient(to bottom right, #d32144, #151fb4);
}
.title {
  text-align: center;
  font-size: 24px;
  margin: 20px auto;
}
.checkcode{
  cursor:pointer;
}
</style>>